var container = document.getElementById("container");
container.addEventListener("touchmove" , function(event){
	event.preventDefault();
	})


String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");

}

function checkPhone(phone){ 
	if(!(/^1[3|4|5|6|7|8|9]\d{9}$/.test(phone))){ 
	return false; 
	}else{
		return true;
		} 
}




function setCss3 (obj,attrObj) {
   for (var i in attrObj) {
     var newi=i;
	 if(newi.indexOf("-")>0){
	   var num=newi.indexOf("-");
	   newi=newi.replace(newi.substr(num,2),newi.substr(num+1,1).toUpperCase());
	 }
	 obj.style[newi]=attrObj[i];
	 newi=newi.replace(newi.charAt(0),newi.charAt(0).toUpperCase());
	 obj.style["webkit"+newi]=attrObj[i];
	 obj.style["moz"+newi]=attrObj[i];
	 obj.style["o"+newi]=attrObj[i];
	 obj.style["ms"+newi]=attrObj[i];
   }
  }

$(function(){
	//icom.alert("123")
	
	/*$(".content").on("swipeleft" , function(){
		console.log("swipeleft")
		})*/
	
	window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
	var posarr = [0,1,0,1,0,1];
	//障碍物数组
	var objarr=[
		{src:"images/z1.png",type:"kill",width:313,height:298},
		{src:"images/z2.png",type:"kill",width:388,height:344},
		{src:"images/z3.png",type:"kill",width:379,height:222},
		{src:"images/z4.png",type:"kill",width:416,height:274},
		{src:"images/z1.png",type:"kill",width:313,height:298},
		{src:"images/z2.png",type:"kill",width:388,height:344},
		{src:"images/z3.png",type:"kill",width:379,height:222},
		{src:"images/z4.png",type:"kill",width:416,height:274},
	]
	//var objStep=Math.floor(Math.random() * objarr.length);
	
	//金币
	var moneyarr=[
		{src:"images/m1.png",type:"money",width:135,height:135},
		{src:"images/m2.png",type:"money",width:135,height:135},
		{src:"images/m3.png",type:"money",width:135,height:135},
		{src:"images/m4.png",type:"money",width:135,height:135},
	]
	//var moneyStep =Math.floor(Math.random() * moneyarr.length); 
	
	var wh = $(window).height();
	var ww = $(window).width();
	console.log(ww);
	var floorArr=[]; // 地板数组
	var lock=true; // 控制播放暂停
	var fw = (ww/wh)*3660+ww-100; // floor 宽度
	
	console.log("fw" , fw)
	 
	var stage=new C3D.Stage();
	stage.size(ww , wh).material({color:""}).update();
	document.getElementById("gamebox").appendChild(stage.el);
	var content = document.getElementById("content")
	var animal = $(".animal");
	//创建一个立体容器
	var space=new C3D.Sprite();
	space.position(0,330,-3660).rotation(-50,0,0).update();
	stage.addChild(space);
	
	for(var i=0;i<7;i++){
		var z = i*300+i*70;
		createFloor({
			z:z
		})
	}
	
	var antop = wh - (ww/fw)*610; // 小牛距离顶部高度
	var mleft = ww/2; // 小牛距离左边距
	animal.css({bottom:(ww/fw)*610});
	
	//小牛跳动
	var atop = 0;
	var n=0;
	var astep=6; //4  118  4
	
	var sx=0;
	var mx=0;
	var aleft=0;
	//console.log((ww/fw)*660)
	
	function createFloor(options){ // 创建地板
		var floor=new C3D.Plane();
		var z = options.z
		
		floor.name('p1'+z).size(fw,300).position(0,0,z).rotation(60,0,0).material({color:"",image:"images/floor.png"}).update();
		space.addChild(floor)
		
		floorArr.push(floor)
		//addObject(floor)
		
	}
	
	function addObject(floor){
		var objfloor = objarr[Math.floor(Math.random() * moneyarr.length)];
		console.log(objfloor)
		var m=new C3D.Plane();
		m.type = objfloor.type;
		var imgw=objfloor.width;
		var imgh=objfloor.height;
		
		var lr = posarr[Math.floor(Math.random()*(posarr.length))];
		if(lr==0){
			var x = fw/4+imgw/2 // 左侧显示障碍物
		}else{
			var x = fw*0.75 - imgw/3 // 右侧显示障碍物 点
		}
		 m.name('p1').size(imgw*2,imgh*2).position(x,-imgh+50,150).rotation(0,0,0).material({color:"",image:objfloor.src}).update();
		floor.addChild(m) 
		
		//添加金币
		var m2=new C3D.Plane();
		var objmoney = moneyarr[Math.floor(Math.random() * moneyarr.length)]
		m2.type = objmoney.type;
		var imgw2 = objmoney.width;
		var imgh2 = objmoney.height;
		if(lr==0){
			var x2 = fw*0.75 - imgw/3 // 右侧显示金币
		}else{
			var x2 = fw/4+imgw/2 // 左侧显示金币
			
		}
		m2.name('p2').size(imgw2*2,imgh2*2).position(x2,-imgh2+50,150).rotation(0,0,0).material({color:"",image:objmoney.src}).update();
		floor.addChild(m2)
		
		
		
	}
	//floorArr[0].children[0].remove()
	//console.log(floorArr[0])
	//console.log(floorArr[1].children[0])
	/* console.log("left",floorArr[1].children[0].el.getBoundingClientRect().left)
	console.log("top",floorArr[1].children[0].el.getBoundingClientRect().top)
	console.log("width",floorArr[1].children[0].el.getBoundingClientRect().width)
	console.log("height",floorArr[1].children[0].el.getBoundingClientRect().height)
	console.log(floorArr[1].children[0].el) */
	
	
	function moveFloor(fn){ // 移动地板
		for(var i=0;i<floorArr.length;i++){
			floorArr[i].z = floorArr[i].z+8.05
			
			if(floorArr[i].z>=2590){ //2220
				floorArr[i].z = 0;	
			}
			floorArr[i].position(0,0,floorArr[i].z).updateT();
			
			
			
			pzfn(floorArr[i] , i)
			
			
			
			
			
			
			
		}
		
	}
	
	function pzfn(o , i){ // 碰撞监测
		var floortop = o.el.getBoundingClientRect().top;
		var floorWidth = o.el.getBoundingClientRect().width;
		var floorHeight = o.el.getBoundingClientRect().height;
		//console.log(o)
		if(floortop > (antop-floorHeight/2) && floortop<=antop){
			for(var c=0;c<o.children.length;c++){
				
				var cheight = o.children[c].el.getBoundingClientRect().height
				var ctop = o.children[c].el.getBoundingClientRect().top + cheight;
				var cwidth = o.children[c].el.getBoundingClientRect().width;
				var cleft = o.children[c].el.getBoundingClientRect().left;
				
				
				if(mleft>=cleft && mleft<=(cleft+cwidth)){
					if(o.children[c].type=='kill'){
						lock=true;
						setTimeout(function(){
							setCss3(animal[0],{"transform":"translate3d(-50%,0,0)"})
						},10)
						
						
					}else if(o.children[c].type=='money'){
							o.children[c].remove()
						
					}
					//console.log(n)
					/* lock=true;
					if(o.children[c].type=='money'){
						lock=true;
							o.children[c].remove()
						
					} */
				}
			}
		}
	}
	
	
	function animalAnim(){
		
		atop=atop+astep;
		if(atop>150){
			
			astep=-7
		}else if(atop<=0){
			
			astep=7
		}
		setCss3(animal[0],{"transform":"translate3d(-50%,-"+atop+"px,0)"})
		
	}
	

	
	
	animate();
	
	function animate(){
		requestAnimationFrame(animate);
		if(lock){return}
		
		moveFloor()
		animalAnim()
		
		
	}
	
	$(".startBtn").click(function(){
		$(".startbox").css({display:"none"});
		//setTimeout(function(){
			lock=false;
			//animate();
		//},250)
	})
	
//左右拖动小牛
	addEventfn()
	function addEventfn(){
		content.addEventListener("touchstart" , startfn , false)
		content.addEventListener("touchmove" , movefn , false)
		//content.addEventListener("touchend" , endfn , false)
	}
	
	function startfn(e){
		aleft = animal[0].offsetLeft;
		sx = e.touches[0].pageX;
		mx = sx;
	}
	function movefn(e){

		mx = e.touches[0].pageX;
		mleft = aleft+(mx-sx);
		if(mleft<0 || mleft>ww){
			lock=true
			//return;
		}
		animal.css({left:mleft})
	}
	
	
	
	
	
	
	})








